home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / TRAVERSABLE.PY < prev    next >
Encoding:
Python Source  |  2000-08-15  |  8.5 KB  |  224 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''This module implements a mix-in for traversable objects.
  65.  
  66. $Id: Traversable.py,v 1.2.2.2 2000/08/15 17:06:55 evan Exp $'''
  67. __version__='$Revision: 1.2.2.2 $'[11:-2]
  68.  
  69.  
  70. import Acquisition
  71. from AccessControl import getSecurityManager
  72. from string import split, join
  73. from urllib import quote
  74.  
  75. _marker=[]
  76. StringType=type('')
  77.  
  78. class Traversable:
  79.  
  80.     absolute_url__roles__=None # Public
  81.     def absolute_url(self, relative=0):
  82.         try:
  83.             req = self.REQUEST
  84.         except AttributeError:
  85.             req = {}
  86.         rpp = req.get('VirtualRootPhysicalPath', ('',))
  87.         spp = self.getPhysicalPath()
  88.         i = 0
  89.         for name in rpp[:len(spp)]:
  90.             if spp[i] == name:
  91.                 i = i + 1
  92.             else:
  93.                 break
  94.         path = map(quote, spp[i:])
  95.         if relative:
  96.             # This is useful for physical path relative to a VirtualRoot
  97.             return join(path, '/')
  98.         return join([req['SERVER_URL']] + req._script + path, '/')
  99.  
  100.     getPhysicalRoot=Acquisition.Acquired
  101.     getPhysicalRoot__roles__=() # Private
  102.  
  103.     getPhysicalPath__roles__=None # Public
  104.     def getPhysicalPath(self):
  105.         '''Returns a path (an immutable sequence of strings)
  106.         that can be used to access this object again
  107.         later, for example in a copy/paste operation.  getPhysicalRoot()
  108.         and getPhysicalPath() are designed to operate together.
  109.         '''
  110.         path = (self.id,)
  111.         
  112.         p = getattr(self,'aq_inner', None)
  113.         if p is not None: 
  114.             path = p.aq_parent.getPhysicalPath() + path
  115.  
  116.         return path
  117.  
  118.     unrestrictedTraverse__roles__=() # Private
  119.     def unrestrictedTraverse(self, path, default=_marker, restricted=0):
  120.  
  121.         if not path: return self
  122.  
  123.         get=getattr
  124.         N=None
  125.         M=_marker
  126.  
  127.         if type(path) is StringType: path = split(path,'/')
  128.         else: path=list(path)
  129.  
  130.         REQUEST={'TraversalRequestNameStack': path}
  131.         path.reverse()
  132.         pop=path.pop
  133.         if restricted: securityManager=getSecurityManager()
  134.         else: securityManager=None
  135.  
  136.         if not path[-1]:
  137.             # If the path starts with an empty string, go to the root first.
  138.             pop()
  139.             self=self.getPhysicalRoot()
  140.             if (restricted and not securityManager.validateValue(self)):
  141.                 raise 'Unauthorized', name
  142.                     
  143.         try:
  144.             object = self
  145.             while path:
  146.                 name=pop()
  147.  
  148.                 if name[0] == '_':
  149.                     # Never allowed in a URL.
  150.                     raise 'NotFound', name
  151.  
  152.                 if name=='..':
  153.                     o=getattr(object, 'aq_parent', M)
  154.                     if o is not M:
  155.                         if (restricted and not securityManager.validate(
  156.                             object, object,name, o)):
  157.                             raise 'Unauthorized', name
  158.                         object=o
  159.                         continue
  160.  
  161.                 t=get(object, '__bobo_traverse__', N)
  162.                 if t is not N:
  163.                     o=t(REQUEST, name)
  164.                     
  165.                     # Note we pass no container, because we have no
  166.                     # way of knowing what it is
  167.                     if (restricted and not securityManager.validate(
  168.                         object, None, name, o)):
  169.                         raise 'Unauthorized', name
  170.                       
  171.                 else:
  172.                     o=get(object, name, M)
  173.                     if o is not M:
  174.                         if restricted:
  175.                             # waaaa
  176.                             if hasattr(get(object,'aq_base',object), name):
  177.                                 # value wasn't acquired
  178.                                 if not securityManager.validate(
  179.                                     object, object, name, o):
  180.                                     raise 'Unauthorized', name
  181.                             else:
  182.                                 if not securityManager.validate(
  183.                                     object, None, name, o):
  184.                                     raise 'Unauthorized', name
  185.                         
  186.                     else:
  187.                         o=object[name]
  188.                         if (restricted and not securityManager.validate(
  189.                             object, object, None, o)):
  190.                             raise 'Unauthorized', name
  191.  
  192.                 object=o
  193.  
  194.             return object
  195.  
  196.         except:
  197.             if default==_marker: raise
  198.             return default
  199.  
  200.     restrictedTraverse__roles__=None # Public
  201.     def restrictedTraverse(self, path, default=_marker):
  202.         return self.unrestrictedTraverse(path, default, restricted=1)
  203.